package com.shizhanzhe.qdd.home.activity;

import android.annotation.SuppressLint;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.shizhanzhe.qdd.R;
import com.shizhanzhe.qdd.base.UserBean;
import com.shizhanzhe.qdd.home.bean.InCome;
import com.shizhanzhe.qdd.home.dialog.SignDialog;
import com.shizhanzhe.qdd.url.Path;
import com.shizhanzhe.qdd.utils.OkHttpDownloadJsonUtil;
import com.shizhanzhe.qdd.widget.MyGridView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Administrator
 */
public class TiXianActivity extends AppCompatActivity {

    @BindView(R.id.top_back)
    ImageView topBack;
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_person_commit)
    TextView tvPersonCommit;
    @BindView(R.id.et_zfb)
    EditText etZfb;
    /*@BindView(R.id.et_money)
    EditText etMoney;*/
    @BindView(R.id.gridView)
    MyGridView gridView;
    @BindView(R.id.alipay)
    RadioButton alipay;
    @BindView(R.id.wx)
    RadioButton wx;
    @BindView(R.id.rg)
    RadioGroup rg;
    @BindView(R.id.bangding)
    LinearLayout bd;
    @BindView(R.id.tv_bd)
    TextView tv_bd;
    @BindView(R.id.ts)
    TextView ts;
    private List<TiXianM> mMList;
    private String money;
    private SignDialog dialog;

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_ti_xian);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
//                .fitsSystemWindows(true)  //使用该属性,必须指定状态栏颜色
                .statusBarColor(R.color.colorAccent)
                .statusBarDarkFont(false, 0.2f)
                .init();
        topTitle.setText("余额提现");
        ts.setSelected(true);
        tvMoney.setText("¥" + UserBean.balance);
        wx.setChecked(true);
        setListener();
        mMList = MoneyList();
        money = mMList.get(0).getMoney().substring(0, mMList.get(0).getMoney().length() - 1);
        gridView.setAdapter(new TiXianAdapter(this, mMList));

        dialog = new SignDialog(TiXianActivity.this);
        dialog.setListener(new SignDialog.SignListener() {
            @Override
            public void checktrue() {
                dialog.dismiss();
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                cm.setText("zxb5699");
                Toast.makeText(TiXianActivity.this, "客服号复制成功", Toast.LENGTH_SHORT).show();
            }
        });
        bd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (flag) {
                    dialog.show();
                    dialog.showImg(R.mipmap.dialog_tx);
                    dialog.showTX("绑定微信公众号", "微信搜索和关注公众号,绑定后即可进行提现<br><font color='#149CF3'>赚小宝平台(zxb5699)</font>");
                }
            }
        });
    }

    boolean flag = false;
    String kefu = "";

    @Override
    protected void onResume() {
        super.onResume();
        OkHttpDownloadJsonUtil.downloadJson(this, Path.INCOMELIST(UserBean.uid), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onsendJson(String json) {

                if (!"1".equals(json) && !"2".equals(json)) {
                    try {
                        InCome inCome = new Gson().fromJson(json, InCome.class);
                        //UserBean.balance = inCome.getData().getRwsr() + "";
                        //tvMoney.setText("¥" + UserBean.balance);
                        UserBean.gao = inCome.getData().getGaotype();
                    } catch (Exception e) {
                        Toast.makeText(TiXianActivity.this, "数据异常", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(TiXianActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void setListener() {
        topBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        tvPersonCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (flag) {
                    dialog.show();
                    dialog.showImg(R.mipmap.dialog_tx);
                    dialog.showTX("绑定微信公众号", "微信搜索和关注公众号,绑定后即可进行提现<br><font color='#149CF3'>赚小宝平台(zxb5699)</font>");
                } else {
                    if (!"".equals(etZfb.getText().toString())) {
                        if (Double.parseDouble(money) > Double.parseDouble(UserBean.balance)) {
                            Toast.makeText(TiXianActivity.this, "提现金额不得大于当前余额", Toast.LENGTH_SHORT).show();
                        } else {
                            if (alipay.isChecked()) {
                                tixian("支付宝：" + etZfb.getText().toString(), money);
                            } else if (wx.isChecked()) {
                                tixian("微信：" + etZfb.getText().toString(), money);
                            }
                        }
                    } else {
                        Toast.makeText(TiXianActivity.this, "请输入提现账号", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case R.id.alipay:
//                        if (UserBean.gao == 0) {
//                            alipay.setEnabled(false);
//                            wx.setChecked(true);
//                            Toast.makeText(TiXianActivity.this, "首次提现仅限微信", Toast.LENGTH_SHORT).show();
//                        } else {
                        etZfb.setHint("请输入支付宝账号");
//                        }
                        break;
                    case R.id.wx:
                        etZfb.setHint("请输入微信号");
                        break;
                    default:
                        break;
                }
            }
        });
    }

    private void tixian(String zfb, String money) {
        OkHttpDownloadJsonUtil.downloadJson(TiXianActivity.this, Path.sqDrawcash(UserBean.uid, money, zfb), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @Override
            public void onsendJson(String json) {
                if (!"1".equals(json) && !"2".equals(json)) {
                    try {
                        JSONObject jsonObject = new JSONObject(json);
                        if (jsonObject.has("status")) {
                            if (jsonObject.getInt("status") == 1) {
                                final SignDialog dialog = new SignDialog(TiXianActivity.this);
                                dialog.setListener(new SignDialog.SignListener() {
                                    @Override
                                    public void checktrue() {
                                        dialog.dismiss();
                                        finish();
//                                        ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
//                                        cm.setText(kefu);
//                                        Toast.makeText(TiXianActivity.this, "客服号复制成功", Toast.LENGTH_SHORT).show();
                                    }
                                });
                                dialog.show();
                                dialog.showImg(R.mipmap.dialog_tx);
                                //dialog.showTX("提现申请已提交", "如需加速审批请联系客服微信<br><font color='#149CF3'>" + kefu + "</font>");
                                dialog.showMsg("提现申请已提交");
                            } else {
                                Toast.makeText(TiXianActivity.this, jsonObject.getString("info"), Toast.LENGTH_SHORT).show();
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    Toast.makeText(TiXianActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private List<TiXianM> MoneyList() {
        List<TiXianM> datas = new ArrayList<>();
        if (UserBean.gao > 0) {
            datas.add(new TiXianM(1, "10元", true));
            datas.add(new TiXianM(2, "20元", false));
            datas.add(new TiXianM(3, "30元", false));
            datas.add(new TiXianM(4, "50元", false));
            datas.add(new TiXianM(5, "100元", false));
            datas.add(new TiXianM(6, "300元", false));
        } else {
            datas.add(new TiXianM(1, "5元", true));
            datas.add(new TiXianM(2, "10元", false));
            datas.add(new TiXianM(3, "30元", false));
            datas.add(new TiXianM(4, "50元", false));
            datas.add(new TiXianM(5, "100元", false));
            datas.add(new TiXianM(6, "300元", false));
        }
        return datas;
    }

    class TiXianM {
        private int id;
        private String money;
        private boolean check;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getMoney() {
            return money;
        }

        public void setMoney(String money) {
            this.money = money;
        }

        public boolean isCheck() {
            return check;
        }

        public void setCheck(boolean check) {
            this.check = check;
        }

        public TiXianM() {
        }

        public TiXianM(int id, String money, boolean check) {
            this.id = id;
            this.money = money;
            this.check = check;
        }
    }

    private class TiXianAdapter extends BaseAdapter {
        private List<TiXianM> datas;
        private Context mContext;

        public TiXianAdapter(Context mContext, List<TiXianM> datas) {
            this.mContext = mContext;
            this.datas = datas;
        }

        @Override
        public int getCount() {
            return datas.size();
        }

        @Override
        public Object getItem(int i) {
            return datas.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {
            ViewHolder vh;
            if (view == null) {
                vh = new ViewHolder();
                view = LayoutInflater.from(mContext).inflate(R.layout.item_tixian, null);
                vh.rb = (RadioButton) view.findViewById(R.id.rb);
                view.setTag(vh);
            } else {
                vh = (ViewHolder) view.getTag();
            }
            vh.rb.setText(datas.get(i).getMoney());
            if (datas.get(i).isCheck()) {
                vh.rb.setChecked(true);
            } else {
                vh.rb.setChecked(false);
            }
            vh.rb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    for (int j = 0; j < datas.size(); j++) {
                        datas.get(j).setCheck(false);
                    }
                    datas.get(i).setCheck(true);
                    notifyDataSetChanged();
                    money = datas.get(i).getMoney().substring(0, datas.get(i).getMoney().length() - 1);
                }
            });
            return view;
        }

        class ViewHolder {
            public RadioButton rb;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }
}